<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose"  :close-on-click-modal="false">
      <el-form :model="formClient"  ref="formClient" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编号" prop="customerCode" :label-width="formLabelWidth">
              <el-input v-model="formClient.customerCode" placeholder="请输入客户编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customerName" :label-width="formLabelWidth">
              <el-input v-model="formClient.customerName" placeholder="请输入客户名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="助记码" prop="mnemonicCode" :label-width="formLabelWidth">
              <el-input v-model="formClient.mnemonicCode" placeholder="请输入助记码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address" :label-width="formLabelWidth">
              <el-input v-model="formClient.address" placeholder="请输入地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson" :label-width="formLabelWidth">
              <el-input v-model="formClient.contactPerson" placeholder="请输入联系人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone" :label-width="formLabelWidth">
              <el-input v-model="formClient.contactPhone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
              <el-input v-model="formClient.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税号" prop="taxNo" :label-width="formLabelWidth">
              <el-input v-model="formClient.taxNo" placeholder="请输入税号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营范围" prop="businessScope" :label-width="formLabelWidth">
              <el-input v-model="formClient.businessScope" placeholder="请输入经营范围"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="简称" prop="shortName" :label-width="formLabelWidth">
              <el-input v-model="formClient.shortName" placeholder="请输入简称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
              <el-input v-model="formClient.remark" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="bankName" :label-width="formLabelWidth">
              <el-input v-model="formClient.bankName" placeholder="请输入开户银行"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount" :label-width="formLabelWidth">
              <el-input v-model="formClient.bankAccount" placeholder="请输入银行账号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSupplier,//新增供应商
  editSupplier,//修改供应商
} from '@/api/basic/PersonnelInformation.js'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formClient: {
        customerType:'1'
      },
      title: '新增客户',
      formLabelWidth: '140px',
      rules: {
        customerCode: [
          { required: true, message: '请输入单位编号', trigger: 'blur' },
        ],
        customerName: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
        ],
        mnemonicCode: [
          { required: true, message: '请输入助记码', trigger: 'blur' },
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    //获取当前行信息
    openDialog(type, val) {
      this.title = type === 'edit' ? '编辑客户' : '新增客户';
      // 根据类型区分新增和编辑逻辑
      if (type === 'edit' && val) {
        this.formClient = {...val};
      }else{
        this.formClient = {
          customerType:'1'
        }
      }
    },
    //提交表单
    submit() {
      this.$refs.formClient.validate((valid) => {
        if (valid) {
          const apiMethod = this.title === '新增客户' ? addSupplier : editSupplier;
          // 调用相应的 API 方法
          apiMethod(this.formClient).then(res => {
            this.$emit('addSuccess'); // 通知父组件添加成功
            this.$message({
              message: res.data.msg, // 显示成功消息
              type: 'success',
            });
            // 提交成功后可以选择清空表单
            this.handleClose();
          }).catch(err => {
            this.$message({
              message: err?.msg || '操作失败', // 捕获错误信息
              type: 'error',
            });
          });
        } else {
          // 校验失败
          return false;
        }
      });
    },
    // 提取重置表单的逻辑
    handleClose() {
      // 通过事件通知父组件更新visible值，触发update:visible事件
      this.$emit('update:visible', false);
      this.$nextTick(() => {
        this.$refs.formClient.resetFields();
      })
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep.el-dialog__body{
  height: 650px;
  overflow-y: auto;
}
</style>
